<?php
/*********************************************************************\
* This File is a part of BH-PANEL (Breizh-Heberg Panel v2.1)
* Copyright (C) 2008-2009 the BH Developpers : Vincent Giersch <mail@vincordi.fr>, Cyprien Laleau <fanning.fr@gmail.com> and Edwin Cabiten <marmottes44@hotmail.fr>
* See file AUTHORS to get more informations

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
/*********************************************************************/
// Variables ututilisées par les fonctions : 
$nbquery = 0; 
$t_sql = 0;

function connexionbdd() {
        mysql_connect("ns2.breizh-heberg.net","root","")or die(mysql_error());
	mysql_select_db(DATABASE)or die(mysql_error());
}

function deconnexionbdd() {
        mysql_close();
}
function uptime()
{
	$uptime = exec("uptime | cut -d, -f1" );
	ereg("up(.*)", $uptime, $resultat);
	$resultat=trim($resultat[1]);
	if (eregi("day", $resultat))
	{
	$uptime2=exec("uptime | cut -d, -f2" );
	$uptime2=trim($uptime2);
	$uptime=$resultat." ".$uptime2;
	}
	else
	$uptime=$resultat;
	return $uptime;
}

function servsock($domain,$port){
	        $starttime = microtime(true);
	        $file      = @fsockopen ($domain, $port, $errno, $errstr, 10);
	        $stoptime  = microtime(true);
	        $status    = 0;

	        if (!$file) $status = -1; 
	        else {
	            fclose($file);
	            $status = ($stoptime - $starttime) * 1000;
	            $status = floor($status);
	        }
	        return $status;
}

function query($sql) {
        global $nbquery;
		global $t_sql;
        $nbquery++;
		list($usec, $sec) = explode(" ", microtime());
		$tps_a =  ((float)$usec + (float)$sec); 
        $var = mysql_query($sql)or die(mysql_error());
		list($usec, $sec) = explode(" ", microtime());
		$tps_b =  ((float)$usec + (float)$sec); 
		$t_sql = $t_sql + ($tps_b - $tps_a);
		return $var;
}

function generate_config_cache() 
{
	$result = mysql_query('SELECT * FROM config');
	while ($cur_config_item = mysql_fetch_row($result))
		$output[$cur_config_item[0]] = $cur_config_item[1];
	// Enregistre tout dans un fichier
	$fh = fopen(ROOT.'cache_config.php', 'a+');
	fwrite($fh, '<?php'."\n\n".'define(\'BZH_CONFIG_LOADED\', 1);'."\n\n".'$bzh_config = '.var_export($output, true).';'."\n\n".'?>');
	fclose($fh);
}

function du($dir) {
    $res = `du -sk $dir`;
    preg_match( '/\d+/', $res, $KB );
    $mb = round($KB[0]/1024,1);
    return $mb;
}
?>
